/*
 * Part of the ACID Project
 * Developed in 2011 by Morgan Taschuk
 */
package acid.spring.jdbc.repositories;

import acid.spring.db.GeneAlias;
import acid.spring.db.Organism;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.jdbc.core.simple.SimpleJdbcInsert;
import org.springframework.stereotype.Repository;
import org.springframework.util.Assert;

/**
 *
 * @author morgan
 */
@Repository
public class JdbcGeneAliasRepository implements GeneAliasRepository, InitializingBean
{

    @Value("${jdbc.sql.genealiases.queryById}")
    private String queryGeneAliasesByIdQuery;
    @Value("${jdbc.sql.genealiases.queryByAlias.casesensitive}")
    private String queryGeneAliasesByAliasCaseSensitiveQuery;
    @Value("${jdbc.sql.genealiases.queryByAlias.nocase}")
    private String queryGeneAliasesByAliasCaseInsensitiveQuery;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    @Override
    public void afterPropertiesSet() throws Exception
    {
        Assert.notNull(this.jdbcTemplate, "the jdbcTemplate can't be null!");
        Assert.notNull(this.queryGeneAliasesByIdQuery, "the TissueByIdQuery can't be null");
    }
    private RowMapper<GeneAlias> geneAliasRowMapper = new RowMapper<GeneAlias>()
    {

        @Override
        public GeneAlias mapRow(ResultSet resultSet, int i) throws SQLException
        {
            String blank = "This space intentionally left blank";
            int id = resultSet.getInt("gene_alias_id");
            String alias = resultSet.getString("alias");
            Organism organism = new Organism(resultSet.getInt("organism_id"), blank, blank);
            return new GeneAlias(id, alias, organism);
        }
    };

    @Override
    public GeneAlias getGeneAliasById(int id)
    {
        return jdbcTemplate.queryForObject(queryGeneAliasesByIdQuery, geneAliasRowMapper, id);
    }

    @Override
    public GeneAlias getGeneAliasByAlias(String s)
    {
        GeneAlias ge=null;
        try
        {
             ge = jdbcTemplate.queryForObject(queryGeneAliasesByAliasCaseSensitiveQuery, geneAliasRowMapper, s);
        }
        catch (EmptyResultDataAccessException e)
        {
            ge = jdbcTemplate.queryForObject(
                    queryGeneAliasesByAliasCaseInsensitiveQuery, geneAliasRowMapper, s);
        }
        return ge;
    }

    @Override
    public GeneAlias saveGeneAlias(GeneAlias geneAlias)
    {

        SimpleJdbcInsert simpleJdbcInsert = new SimpleJdbcInsert(jdbcTemplate);
        simpleJdbcInsert.setTableName("gene_alias");
        simpleJdbcInsert.setColumnNames(Arrays.asList("alias", "organism_id"));
        simpleJdbcInsert.setGeneratedKeyName("gene_alias_id");
        Map<String, Object> args = new HashMap<String, Object>();
        //args.put("gene_alias_id", geneAlias.getGene_alias_id());
        args.put("alias", geneAlias.getAlias());
        args.put("organism_id", geneAlias.getOrganism().getOrganism_id());

        Number id = simpleJdbcInsert.execute(args);
        return getGeneAliasById(id.intValue());
    }
}
